using System;
using System.Collections;
using System.Data;
using CE.Database;
/// <summary>
/// Summary description for UserProfileDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarDrivingExpDB
    {
        public CarDrivingExpDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarDrivingExp cardrivingexp)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarDrivingExpInsertUpdateSingleItem]";
            
            SortedList _sPara = new SortedList();
            
            #region Parameter
            _sPara.Add("@ProviderCarDrivingExpId", cardrivingexp.ProviderCarDrivingExpId);
            _sPara.Add("@ProviderCompanyId", cardrivingexp.ProviderCompanyId);
            _sPara.Add("@FromDrivingExpYear", cardrivingexp.FromDrivingExpYear);
            _sPara.Add("@ToDrivingExpYear", cardrivingexp.ToDrivingExpYear);
            _sPara.Add("@DrivingExpRate", cardrivingexp.DrivingExpRate);
            _sPara.Add("@RefCEUserId", cardrivingexp.RefCEUserId);
            _sPara.Add("@Status", cardrivingexp.Status);
            _sPara.Add("@Finfo1", cardrivingexp.FInfo1);
            _sPara.Add("@Dinfo1", cardrivingexp.DInfo1);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Update()
        {
            return true;
        }

        public bool Delete(Int64 ProviderCarDrivingExpId, Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarDrivingExpInfoDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarDrivingExpId", ProviderCarDrivingExpId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;
        }

        public CarDrivingExpList GetProviderCarDrivingExpList(Int64 ProviderCompanyId)
        {
            CarDrivingExpList _cardrivingexplist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarDrivingExpSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _cardrivingexplist = new CarDrivingExpList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        CarDrivingExp _cardrivingexp = new CarDrivingExp();
                        _cardrivingexp = FillDataRecord(_dt, i);
                        _cardrivingexplist.Add(_cardrivingexp);
                    }
                }
            }
            return _cardrivingexplist;
        }

        public CarDrivingExp GetProviderCarDrivingExp(Int64 ProviderCarDrivingExpId)
        {
            CarDrivingExp _cardrivingexp = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarDrivingExpSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarDrivingExpId", ProviderCarDrivingExpId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _cardrivingexp = new CarDrivingExp();
                    _cardrivingexp = FillDataRecord(_dt, 0);
                }
            }
            return _cardrivingexp;
        }

        private CarDrivingExp FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarDrivingExp myCarDrivingExp = new CarDrivingExp();
            myCarDrivingExp.ProviderCarDrivingExpId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarDrivingExpId"].ToString());
            myCarDrivingExp.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarDrivingExp.FromDrivingExpYear = Convert.ToInt32(myDataTable.Rows[RowId]["FromDrivingExpYear"].ToString());
            myCarDrivingExp.ToDrivingExpYear = Convert.ToInt32(myDataTable.Rows[RowId]["ToDrivingExpYear"].ToString());
            myCarDrivingExp.DrivingExpRate = Convert.ToSingle(myDataTable.Rows[RowId]["DrivingExpRate"].ToString());
            myCarDrivingExp.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarDrivingExp.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarDrivingExp.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarDrivingExp.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarDrivingExp.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            return myCarDrivingExp;
        }
        
    }
}